package dji.ux.ext.setting.aircraft;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

//import dji.common.flightcontroller.flightassistant.BottomAuxiliaryLightMode;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProvider;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;
import dji.ux.ext.base.view.SelectedOptionPanel;

public class AuxiliaryLightPanel extends ConstraintLayoutWidget {
    private SchedulerProvider schedulerProvider;
    private AuxiliaryLightModel auxiliaryLightModel;
    private Button btn;
    public AuxiliaryLightPanel(@NonNull Context context) {
        super(context);
    }

    public AuxiliaryLightPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AuxiliaryLightPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_auxiliary_light, this);
        btn = view.findViewById(R.id.button);
        btn.setOnClickListener(view1 -> showPopWindow(btn));
        schedulerProvider = SchedulerProvider.getInstance();
        setClickable(true);
        setFocusable(true);

        if (!isInEditMode()) {
            auxiliaryLightModel = new AuxiliaryLightModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance(),
                    schedulerProvider);
        }

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    private void showPopWindow(View view) {
        PopupWindow popupWindow = new PopupWindow();
        SelectedOptionPanel panel = new SelectedOptionPanel(getContext());
        panel.setCallback((index, result) -> {
//            setMode(index);
            popupWindow.dismiss();
        });

        panel.setCurrentText(((Button) view).getText().toString().trim());

        List<String> data = new ArrayList<>();
        data.add("自动");
        data.add("开启");
        data.add("关闭");
        data.add("夜航灯模式");
        panel.setData(data);

        popupWindow.setContentView(panel);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setWidth(view.getMeasuredWidth());
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.showAsDropDown(view, 0, view.getMeasuredHeight());
    }

    @Override
    protected void reactToModelChanges() {
//        addReaction(auxiliaryLightModel.getBottomAuxiliaryLightMode()
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(this::updateText));
    }

//    private void updateText(BottomAuxiliaryLightMode bottomAuxiliaryLightMode){
//        switch (bottomAuxiliaryLightMode.value()) {
//            case 0://自动
//               btn.setText("自动");
//                break;
//            case 1://开启
//                btn.setText("开启");
//                break;
//            case 2://关闭
//                btn.setText("关闭");
//                break;
//            case 3://夜航灯模式
//                btn.setText("夜航灯模式");
//                break;
//        }
//    }
//
//    private void setMode(int index){
//        switch (index){
//            case 0:
//                setAuxiliaryLightModel(BottomAuxiliaryLightMode.AUTO);
//                btn.setText("自动");
//                break;
//            case 1:
//                setAuxiliaryLightModel(BottomAuxiliaryLightMode.ON);
//                btn.setText("开启");
//                break;
//            case 2:
//                setAuxiliaryLightModel(BottomAuxiliaryLightMode.OFF);
//                btn.setText("关闭");
//                break;
//            case 3:
//                setAuxiliaryLightModel(BottomAuxiliaryLightMode.BEACON);
//                btn.setText("夜航灯模式");
//                break;
//        }
//    }
//
//    private void setAuxiliaryLightModel(BottomAuxiliaryLightMode model){
//        addDisposable(auxiliaryLightModel.setAuxiliaryLightMode(model).subscribe(() -> {
//            // Do nothing
//        }, logErrorConsumer(TAG, "setAuxiliaryLightModel mode: ")));
//    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            auxiliaryLightModel.setup();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            auxiliaryLightModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }
}
